/* Copyright 2009 Matthew Lloyd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtGui>
#include <QGridLayout>

#include "DoomEdit.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


MainWindow::MainWindow()
{
    textEdit = new QTextEdit;
    setCentralWidget(textEdit);
    QWidget::setWindowIcon(QIcon("skull.png"));
    createActions();
    createMenus();
    createStatusBar();
    createToolBars();
    createSysTray();
    setWindowTitle(tr("DoomEdit 1.0"));
}

void MainWindow::about()
{
   QMessageBox::about(this, tr("DoomEdit 1.0"),
            tr("DoomEdit 1.0 - This program was written for myself to learn Qt by trying things out. If you find it useful, then share it. Feedback is always welcome. This program is distributed under the terms of the GPL v3. See file COPYING.TXT for more details.                   Written by Matthew Lloyd"));
}

void MainWindow::aboutyourdoom()
{
        QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(1);
        trayIcon->showMessage("Doom","Doom to you champion!", icon, 15000);
        QMessageBox::about(this, tr("About YOUR DOOM"), tr("You are REALLY DOOMED!"));
}


void MainWindow::createActions()
{

    aboutAct = new QAction(QIcon("about.png"),tr("&About"), this);
    aboutAct->setStatusTip(tr("About this program"));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

    aboutQtAct = new QAction(QIcon("qt.png"),tr("About &Qt"), this);
    aboutQtAct->setStatusTip(tr("About Qt"));
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

    doomAct = new QAction(QIcon("skull.png"),tr("&Doom"), this);
    doomAct->setStatusTip(tr("You are DOOMED!"));
    doomAct->setShortcut(tr("Ctrl+D"));
    connect(doomAct, SIGNAL(triggered()), this, SLOT(aboutyourdoom()));

    exitAct = new QAction(QIcon("error.png"),tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    exitAct->setStatusTip(tr("Exit the application"));
    connect(exitAct, SIGNAL(triggered()), qApp, SLOT(quit()));

    saveAct = new QAction(QIcon("save.png"), tr("&Save"), this);
    saveAct->setShortcut(tr("Ctrl+S"));
    saveAct->setStatusTip(tr("Save the document to disk"));
    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

    openAct = new QAction(QIcon("open.png"),tr("Open"),this);
    openAct->setShortcut(tr("Ctrl+O"));
    openAct->setStatusTip(tr("Open File"));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(openAct);
    fileMenu->addAction(saveAct);
    fileMenu->addAction(exitAct);
   

    menuBar()->addSeparator();

    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);
    helpMenu->addAction(doomAct);
   
}

void MainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("Ready To Kick Ass"));
}

void MainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);
    fileToolBar->addAction(exitAct);
    fileToolBar->addAction(aboutAct);
    fileToolBar->addAction(doomAct);

}

void MainWindow::createSysTray()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(exitAct);

    trayIcon = new QSystemTrayIcon(QIcon("skull.png"),this);
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->show();
}


bool MainWindow::save()
{
    if (curFile.isEmpty()) {
        return saveAs();
    } else {
        return saveFile(curFile);
    }
}

bool MainWindow::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this);
    if (fileName.isEmpty())
        return false;

    return saveFile(fileName);
}

void MainWindow::open()
{
    if (maybeSave()) {
        QString fileName = QFileDialog::getOpenFileName(this);
        if (!fileName.isEmpty())
            loadFile(fileName);
    }
}

bool MainWindow::maybeSave()
{
    if (textEdit->document()->isModified()) {
        QMessageBox::StandardButton ret;
        ret = QMessageBox::warning(this, tr("Application"),
                     tr("The document has been modified.\n"
                        "Do you want to save your changes?"),
                     QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        if (ret == QMessageBox::Save)
            return save();
        else if (ret == QMessageBox::Cancel)
            return false;
    }
    return true;
}

void MainWindow::loadFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        QMessageBox::warning(this, tr("Application"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(fileName)
                             .arg(file.errorString()));
        return;
    }

    QTextStream in(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    textEdit->setPlainText(in.readAll());
    QApplication::restoreOverrideCursor();

    setCurrentFile(fileName);
    statusBar()->showMessage(tr("File loaded"), 2000);
}

bool MainWindow::saveFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, tr("Application"),
                             tr("Cannot write file %1:\n%2.")
                             .arg(fileName)
                             .arg(file.errorString()));
        return false;
    }

    QTextStream out(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    out << textEdit->toPlainText();
    QApplication::restoreOverrideCursor();


    setCurrentFile(fileName);
    statusBar()->showMessage(tr("File saved"), 2000);
    return true;
}

void MainWindow::setCurrentFile(const QString &fileName)
{
    curFile = fileName;
    textEdit->document()->setModified(false);
    setWindowModified(false);

    QString shownName;
    if (curFile.isEmpty())
        shownName = "untitled.txt";
    else
        shownName = strippedName(curFile);

    setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("DoomEdit 0.4 Beta")));
}

QString MainWindow::strippedName(const QString &fullFileName)
{
    return QFileInfo(fullFileName).fileName();
}
